const app = getApp()
const api = wx.getStorageSync('api')
let user
Page({
  data: {
    address: null
  },

  onLoad: function (options) {
    user = wx.getStorageSync('user')
    this.getAddress()
  },

  onShow() {
    this.getAddress()
  },

  getAddress() {
    const url = api.address_get + `?userId=${user.id}`
    app.$http.get(url).then(res => {
      // console.log(res)
      this.setData({
        address: res.data
      })
    })
  },

  // 选择收获地址
  chooseAddress() {
    let that = this;
    wx.chooseAddress({
      success(res) {
        let address = {
          userId: user.id,
          provinceName: res.provinceName,
          cityName: res.cityName,
          countyName: res.countyName,
          detailedAddress: res.detailInfo,
          userName: res.userName,
          telNumber: res.telNumber,
          postalCode: res.postalCode
        }
        console.log(address)
        const url = api.address_save
        app.$http.post(url, address).then(res => {
          that.getAddress()
          console.log(res)
        })
      }
    })
  }
})